/*
 * @Descripttion: 
 * @version: 
 * @Author: 刘冰见
 * @Date: 2024-10-08 22:44:24
 * @LastEditors: 刘冰见
 * @LastEditTime: 2024-10-21 11:13:14
 */
import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:sousuoapp/common/auth.dart';
import 'package:sousuoapp/pages/order/order.dart';
import 'package:sousuoapp/pages/login/login.dart';

void main() async {
  await GetStorage.init();
  final auth = Auth();
  // 检测是否登录
  runApp(ChangeNotifierProvider.value(
    value: auth,
    child: const MyApp(),
  ));
  // runApp(const MyApp());
}

// 路由配置

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    final auth = context.watch<Auth>();
    // if (!auth.isAuthenticated) {
    //   context.go('/login');
    // }
    return MaterialApp.router(
      routerConfig: GoRouter(
        routes: <RouteBase>[
          GoRoute(
            path: '/',
            builder: (BuildContext context, GoRouterState state) {
              return const OrderScreen();
            },
            routes: <RouteBase>[
              GoRoute(
                path: '/login',
                builder: (BuildContext context, GoRouterState state) {
                  return const LoginPage();
                },
              ),
            ],
          ),
        ],
        redirect: (BuildContext context, GoRouterState state) {
          if (!auth.isAuthenticated) {
            return '/login';
          } else {
            return '/';
          }
        },
      ),
    );
  }
}
